<?php
namespace backend\controllers;


use backend\components\Controller;
use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\form\AdminLoginForm;


/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','welcome','tt'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    //'logout' => ['post'],
                ],
            ],
        ];
    }
    public $layout='base';
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

  /**
   * Displays homepage.
   *
   * @return string
   */
  public function actionWelcome()
  {
    return $this->render('welcome');
  }



  /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $model = new AdminLoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }


  public function actionTt()
  {
    $auth = Yii::$app->authManager;
    $auth->removeAllAssignments();//删除授权
    $auth->removeAll();//删除权限


    // 添加 "index" 权限
    $indexNews = $auth->createPermission('news/index');
    $indexNews->description = '新闻列表';
    $auth->add($indexNews);

    // 添加 "create" 权限
    $createNews = $auth->createPermission('news/create');
    $createNews->description = '新闻添加';
    $auth->add($createNews);

    // 添加 "update" 权限
    $updateNews = $auth->createPermission('news/update');
    $updateNews->description = '新闻更新';
    $auth->add($updateNews);

    // 添加 "delete" 权限
    $deleteNews = $auth->createPermission('news/delete');
    $deleteNews->description = '新闻删除';
    $auth->add($deleteNews);



    // 添加 "author" 角色并赋予 "createNews" 权限
    $author = $auth->createRole('author');
    $auth->add($author);
    $auth->addChild($author, $createNews);
    $auth->addChild($author, $indexNews);
    $auth->addChild($author, $updateNews);

    // 添加 "admin" 角色并赋予 "deleteNews"
    // 和 "author" 权限
    $admin = $auth->createRole('admin');
    $auth->add($admin);
    $auth->addChild($admin, $deleteNews);
    $auth->addChild($admin, $author);

    // 为用户指派角色。其中 1 和 2 是由 IdentityInterface::getId() 返回的id （译者注：user表的id）
    // 通常在你的 User 模型中实现这个函数。
    $auth->assign($author, 2);
    $auth->assign($admin, 1);







    exit;
    $this->module->set('ac',['class'=>'common\services\AdminContext']);
    list($siteName,$key) = $this->module->get('ac')->doCallTest();
    list($siteName2,$key2) = $this->module->get('ac')->doCallTest();
    echo $siteName . '--key--' . $key;
    echo $siteName2 . '--key--' . $key2;

    exit;
    var_dump($this->module);exit;
    //$cache = Yii::$app->getCache() ;

   // $cache->add('name','zhangsan');
   // $cache->add('test','lisi');

    //$cache->set('t1','lisi');
    //var_dump($cache->get('t1'));
    Yii::$app->redis->set('a','1');
      //Yii::$app->redis->set();
    //Yii::$app->redis->set('t','1');

   var_dump(Yii::$app->redis->get('a'));

  }

}
